﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.RaiseManagement;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using System;
using System.Reflection;
using System.Threading.Tasks.Dataflow;
using System.Xml;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace FarmSmartSleep.Organ.API.Application.RaiseCommands
{
    public class RaiseSelectHandler : IRequestHandler<RaiseSelectCommand, Result>
    {
        private readonly IBaseRepository<SheepRaiseDeath> sheepRaiseDeathRepository;//死亡管理
        private readonly IBaseRepository<SheepDeath> sheepDeathepository;//死亡原因
        private readonly IBaseRepository<SleepInfor> sleepinforRepository;//羊只管理
        private readonly IBaseRepository<Cottages> cottagesRepository;//栋舍管理
        private readonly IBaseRepository<Fields> fieldsRepository;//栏位管理
        private readonly IBaseRepository<Variety> varietyRepository;//品种管理

        public RaiseSelectHandler(IBaseRepository<SheepRaiseDeath> sheepRaiseDeathRepository, IBaseRepository<SleepInfor> sleepinforRepository, IBaseRepository<Cottages> cottagesRepository, IBaseRepository<Fields> fieldsRepository, IBaseRepository<Variety> varietyRepository, IBaseRepository<SheepDeath> sheepDeathepository)
        {
            this.sheepRaiseDeathRepository = sheepRaiseDeathRepository;
            this.sleepinforRepository = sleepinforRepository;
            this.cottagesRepository = cottagesRepository;
            this.fieldsRepository = fieldsRepository;
            this.varietyRepository = varietyRepository;
            this.sheepDeathepository = sheepDeathepository;
        }

        public async Task<Result> Handle(RaiseSelectCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.success };
            try
            {
                var list = (from a in sleepinforRepository.GetAll()
                            join b in sheepRaiseDeathRepository.GetAll() on a.SleepInforId equals b.SleepInforId
                            join c in sheepDeathepository.GetAll() on b.SheepDeathId equals c.SheepDeathId
                            join d in cottagesRepository.GetAll() on a.RecordcottageId equals d.CottagesId
                            join e in fieldsRepository.GetAll() on a.FieIdId equals e.FieIdId
                            join f in varietyRepository.GetAll() on a.VarietyId equals f.VarietyId
                            select new SheepRaiseDeathDto()
                            {
                                SheepRaiseDeathId = b.SheepRaiseDeathId,
                                SheepRaiseDeathData = b.SheepRaiseDeathData,
                                SleepInforId = a.SleepInforId,
                                EarNumber = a.EarNumber,
                                SheepDeathId = c.SheepDeathId,
                                SheepDeathName = c.SheepDeathName,
                                SheepRaiseDeathCode = b.SheepRaiseDeathCode,
                                VarietyId = f.VarietyId,
                                VarietyName = f.VarietyName,
                                Gender = a.Gender,
                                BreedStatuId = a.BreedStatuId,
                                CottagesId = d.CottagesId,
                                CottageName = d.CottageName,
                                FieIdId = e.FieIdId,
                                FieIdName = e.FieIdName,
                                SleepBirthDate = a.SleepBirthDate,
                                CreateId = b.CreateId,
                                CreateTime = b.CreateTime,
                                UpdateId = b.UpdateId,
                                UpdateTime = b.UpdateTime,
                                ExamId = b.ExamId,
                                ExamTime = b.ExamTime,
                                AgeDay = (int)(DateTime.Now - Convert.ToDateTime(b.SheepRaiseDeathData)).TotalDays,
                            }).ToList();
                //死亡日期
                if (!string.IsNullOrEmpty(request.start))
                {
                    list = list.Where(x => x.SheepRaiseDeathData >= DateTime.Parse(request.start)).ToList();
                }
                if (!string.IsNullOrEmpty(request.end))
                {
                    list = list.Where(x => x.SheepRaiseDeathData < DateTime.Parse(request.end)).ToList();
                }
                if (request.sleepInforId > 0)
                {
                    list = list.Where(x => x.SleepInforId == request.sleepInforId).ToList();
                }
                //死亡原因
                if (request.sheepDeathId != 0)
                {
                    list = list.Where(x => x.SheepDeathId == request.sheepDeathId).ToList();
                }
                if (list != null)
                {
                    result.Code = (int)CodeEnum.success;
                    result.Data = list;
                }
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
